<?php
namespace app\user\service;
use app\common\library\Service;
use EasyWeChat\Factory;

class User extends Service
{
    use \app\common\library\traits\Model;

    protected $model = null;

    protected function _initialize() {
        parent::_initialize();
        $this->model = model('user/user');
    }

    /**
     * 注册微信用户
     * @return bool
     */
    public function regiser($info) {
        if(!$info['openid']) {
            $this->error = '参数错误';
            return false;
        }
        $maps = ['openid' => $info['openid']];
        if($info['unionid']) {
            $maps['unionid'] = $info['unionid'];
        }
        $user = $this->model->where($maps)->find();
        if(!$user) {
            $config = config('wechat.mini_program');
            $miniProgram = Factory::miniProgram($config);
            $response = $miniProgram->app_code->get('/pages/index/index?openid='.$info['openid']);
            $filename = $response->saveAs('attachment/qrcode/', $info['openid'].'.png');
            $info['unionid'] = (string) $info['unionid'];
            $info['qrcode'] = config('upload.siteurl').'/attachment/qrcode/'.$filename;
            $info['customer_code'] = '';
            $info['customer_realname'] = '';
            $info['customer_pinyin'] = '';
            $info['token'] = \fast\Random::uuid();
            $user = $this->save($info, []);
        }
        if(!$user) {
            $this->error = $this->getError();
            return false;
        }
        \think\Hook::listen("user_register_after", $user);
        return $user;
    }

    /**
     * 用户积分变动
     * @param $user_id
     * @param int $point
     * @param string $type
     * @param string $msg
     * @param string $order_sn
     * @return boolean
     */
    public function point($user_id, $point = 0, $type = 'goods', $msg = '', $order_sn = '') {
        $result = $this->where(['id' => $user_id])
            ->setInc('point', $point);

        \think\Db::startTrans();
        if(!$result) {
            \think\Db::rollback();
            $this->error = $this->getError();
            return false;
        }
        // 写入日志
        $log = [
            'uid' => (int) $user_id,
            'type' => (string) $type,
            'point' => (int) $point,
            'msg' => (string) $msg,
            'order_no' => (string) $order_sn,
        ];
        $log_result = model('user/user_point_log')->save($log);
        if(!$log_result) {
            \think\Db::rollback();
            $this->error = model('user/user_point_log')->getError();
            return false;
        }
        \think\Db::commit();
        return $result;
    }


    /**
     * 计算权重
     * @param integer $user_id
     * @return boolean
     */
    public function weight($user_id, $weight = false) {
        if($weight === false) {
            $formula = "{{落地率}} * 0.6 + {{响应率}} * 0.4";
            $replace = [
                '落地数' => 0,
                '跟进数' => 0,
                '响应数' => 0,
                '个人池总数' => 0
            ];
        }

        $user = $this->model->find($user_id);
        if(!$user) {
            $this->error = '用户不存在';
            return false;
        }
        $user->weight = $weight;

        if(!$user->save()) {
            $this->error = $user->getError();
            return false;
        }

        return $user;
    }

}
